
#pragma once

#include <AVUIFrameworkElement.h>
#include <AVUIUIElementCollection.h>

namespace AVUI {

class GridViewColumnCollection;
class GridViewColumn;
class GridViewColumnCollectionChangedEventArgs;
class CollectionChangedEventArgs;

class GridViewRowPresenterBase : public FrameworkElement
{
public:
    GridViewRowPresenterBase() : m_children(this), m_needUpdateVisualTree(true) { };
    ~GridViewRowPresenterBase();

    DECLARE_ELEMENT(GridViewRowPresenterBase, FrameworkElement);

    DECLARE_DEPENDENCY_PROPERTY(Columns);
    TRefCountedPtr<GridViewColumnCollection> get_Columns() { return (GridViewColumnCollection*)(Object*)GetValue(get_ColumnsProperty()); }
    void set_Columns(GridViewColumnCollection* pValue) { SetValue(get_ColumnsProperty(), (Object*)pValue); } 

protected:
    bool get_NeedUpdateVisualTree() { return m_needUpdateVisualTree; }
    void set_NeedUpdateVisualTree(bool needUpdateVisualTree) { m_needUpdateVisualTree = needUpdateVisualTree; }

    int get_VisualChildrenCount() { return m_children.get_Count(); }
    Visual* GetVisualChild(int idx) { return m_children[idx]; }

    void EnsureDesiredWidthList();

    UIElementCollection* get_InternalChildren() { return &m_children; }
    virtual void OnColumnPropertyChanged(GridViewColumn* pColumn, Property* pProperty) = 0;
    virtual void OnColumnCollectionChanged(GridViewColumnCollectionChangedEventArgs* pArgs);

    virtual TRefCountedPtr<Object> get_LogicalChildren() { return m_children.GetEnumerator(); }

    ItemStructList<float> m_desiredWidthList;

private:
    static void StaticConstructor();

    bool get_IsPresenterVisualReady() { return get_IsInitialized() && !get_NeedUpdateVisualTree(); }
    void ColumnCollectionChanged(Object* pSender, CollectionChangedEventArgs* pArg);
    static void ColumnsPropertyChanged(DependencyObject* pDO, DependencyPropertyChangedEventArgs* e);

    DependencyObject* GetStableAncestor();

    UIElementCollection m_children;

    bool m_needUpdateVisualTree : 1;

};

}; // namespace AVUI

